package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;

import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyServiceImpl implements IStrategyService{
    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if(strategy.getId() == null){
            strategyMapper.insert(strategy);
        }else {
            strategyMapper.updateByPrimaryKey(strategy);
        }
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Strategy> list = strategyMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Map> listAll() {
        return strategyMapper.listAll();
    }

    @Override
    public List<Strategy> listAllByState(QueryObject qo) {
        return strategyMapper.listAllByState(qo);
    }

    @Override
    public PageInfo queryStrategyByPlaceId(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Strategy> list = strategyMapper.listAllByState(qo);
        return new PageInfo(list);
    }

    @Override
    public Map<String, Object> selectPraiseNums(Long strategyId,Integer state) {

        Integer strategies = strategyMapper.selectPraiseNums(strategyId,state);
        Map<String,Object> map = new HashMap<>();
        map.put("strategies",strategies);
        return map;
    }

    @Override
    public void insertRelation(Long userId, Long strategyId, Integer state) {
        strategyMapper.insertRelations(userId,strategyId,state);
    }

    @Override
    public Boolean selectRelation(Long userId, Long strategyId, Integer state) {
        String s = strategyMapper.selectRelation(userId, strategyId, state);
        if (s == null) {
            return false;
        }
        return true;
    }

    @Override
    public void deleteRelation(Long strategyId, Long userId, Integer state) {
        strategyMapper.deleteRelation(strategyId,userId,state);
    }

    @Override
    public PageInfo queryByKeyword(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Strategy> list = strategyMapper.queryForList(qo);
        return new PageInfo(list);
    }


}
